package com.spark.tags;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class InputField extends Field {
	
	public static final String CLASSNAME_WRAP = "s_w";//item-wrap
	public static final String CLASSNAME_EMPTYTEXT = "s_et";//item-emptyText
	
	public static final String PROPERTITY_PLACEHOLDER = "placeholder";
	public static final String PROPERTITY_LINE_HEIGHT = "lineHeight";
	public static final String PROPERTITY_INPUT_HEIGHT = "inputHeight";//TIP:所有输入框的光标高度，通过减少height增加上下padding来统一
	public static final String PROPERTITY_INPUTWIDTH = "inputWidth";
	public static final String PROPERTITY_INPUTSTYLE = "inputStyle";
//	public static final String PROPERTITY_LINE_HEIGHT = "lineHeight";
	
	protected String getDefaultClass(XMap view,SparkContext context){
		String wrapClass = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(wrapClass);
		if(!"".equals(wrapClass.trim()))sb.append(" ");
		sb.append(CLASSNAME_WRAP);
		
				
		String emptyText = view.getString(PROPERTITY_PLACEHOLDER,"");
		String value = (String)context.get(PROPERTITY_VALUE);
		if(!"".equals(emptyText) && "".equals(value)) {
			sb.append(" ");
			sb.append(CLASSNAME_EMPTYTEXT);
		}
		return sb.toString();
	}
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		
		/** 输入框宽度**/
		int width = (Integer)context.get(PROPERTITY_WIDTH);
		context.put(PROPERTITY_INPUTWIDTH, new Integer(width-5));
		
		/** HEIGHT LINE-HEIGHT**/
		int height = (Integer)context.get(PROPERTITY_HEIGHT);
		context.put(PROPERTITY_INPUT_HEIGHT, new Integer(height-2));
		context.put(PROPERTITY_LINE_HEIGHT, new Integer(height-2));//-4
		
		
		
		/** 值 **/
		String value = (String)context.get(Component.PROPERTITY_VALUE);
		
		/** inputStyle **/
		String inputStyle = view.getString(PROPERTITY_INPUTSTYLE);
		if(inputStyle != null) {
			context.put(PROPERTITY_INPUTSTYLE, inputStyle);
		}
		
		/** 文本提示 **/
		String emptyText = view.getString(PROPERTITY_PLACEHOLDER,"");
		if(!"".equals(emptyText) && "".equals(value)) {
			context.put(Component.PROPERTITY_VALUE, emptyText);
			context.addJsonConfig(InputField.PROPERTITY_PLACEHOLDER, emptyText);
		}
		
	}
}
